package com.jyb.qiyefuwu.view.activity.usercenter;


import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.tabs.TabLayout;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.ActivityDatabaseBinding;
import com.jyb.qiyefuwu.uitls.CommPagerAdapter3;
import com.jyb.qiyefuwu.uitls.DialogFunction;
import com.jyb.qiyefuwu.uitls.DialogHelper;
import com.jyb.qiyefuwu.view.fragment.usercenter.DatabaseListFragment;
import com.jyb.qiyefuwu.viewmodel.SalesOderDetailsModel;
import com.sgb.link.router.PathEnterpriseServices;
import com.swgk.core.common.base.BaseActivity;

import java.util.ArrayList;


/**
 * 作者:陈方凯
 * 日期:2021/10/29
 * 说明:凭证
 */
@Route(path = PathEnterpriseServices.DatabaseActivity)
public class DatabaseActivity extends BaseActivity<SalesOderDetailsModel, ActivityDatabaseBinding> {
    private String[] type = new String[]{"买方", "卖方"};
    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private TextView selectTextView;
    private String orderId;
    private String orderstatus;//订单状态

    @Override
    protected int layoutId() {
        return R.layout.activity_database;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(SalesOderDetailsModel.class);
    }

    @Override
    protected void init() {
        orderId = getIntent().getStringExtra("id");
        mViewModel.buyerOrSeller = getIntent().getIntExtra("buyerOrSeller", 0);
        orderstatus = getIntent().getStringExtra("orderstatus");
        mBinding.backView.setOnClickListener(this);
        mBinding.tvCommit.setOnClickListener(this);
        mBinding.tvCommit.setText("新增");
        if (mViewModel.buyerOrSeller == 0) {
            //买方进入界面
            fragmentList.add(DatabaseListFragment.newInstance(0, orderId, (!"3".equals(orderstatus) && !"5".equals(orderstatus)) ? false : true));
            fragmentList.add(DatabaseListFragment.newInstance(1, orderId, true));
        } else {
            //卖方进入界面
            fragmentList.add(DatabaseListFragment.newInstance(0, orderId, true));
            fragmentList.add(DatabaseListFragment.newInstance(1, orderId, !"3".equals(orderstatus) || !"5".equals(orderstatus) ? false : true));
        }
        CommPagerAdapter3 pagerAdapter = new CommPagerAdapter3(getSupportFragmentManager(), fragmentList, type);
        mBinding.viewpager.setOffscreenPageLimit(type.length);
        mBinding.viewpager.setAdapter(pagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewpager);
        mBinding.viewpager.setCurrentItem(0);
       /* LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(DatabaseActivity.this).inflate(R.layout.item_lin_red, null);
        TextView selectTextView = linearLayout.findViewById(R.id.tab_item_textview);
        selectTextView.setText(mBinding.tabLayout.getTabAt(0).getText());
        mBinding.tabLayout.getTabAt(0).setCustomView(linearLayout);*/
        /* mBinding.tabLayout.getTabAt(0).select();*/
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                showTitleRightText(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        showTitleRightText(0);
    }

    public void showTitleRightText(int position) {
        if (!TextUtils.isEmpty(orderstatus) && !"3".equals(orderstatus)&&!"5".equals(orderstatus)) {
            if (mViewModel.buyerOrSeller == 1) {
                mBinding.tvCommit.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
            } else {
                mBinding.tvCommit.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
            }
        } else {
            mBinding.tvCommit.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back_view) {
            finish();
        } else if (id == R.id.tv_commit) {
            DialogHelper.showDialog(this, "提示", "请前往PC端上传资料",
                    "", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {

                        }
                    });
        }
    }
}
